#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

//This Unit Test Will Test the Council Room Card.

//initialize game struct to array of 0's
void clearGameState(struct gameState *G) {
	int i;
	for (i=0; i<sizeof(struct gameState); i++) {
		((char *)G)[i] = 0;
	}
}

int main() {
	int i, j, k;
	int currentPlayer, handCount, deckCount;	//relevant to council room card in dominion.c
	int seed = 1003;		//FEEL FREE TO CHANGE
	int num_tests = 100;	//FEEL FREE TO CHANGE
	int errors = 0;			//number of error'd tests
	int debug = 0;
	
	//This test will run by initializing two game states, and comparing them by calling the function on one.
	struct gameState G, G2;
	
	//initialize random number using rngs.c library
	SelectStream(1);
	PutSeed((long)seed);
	
	printf("Now testing cardtest4 (Council Room card)\n");
	
	for (i=0; i<num_tests; i++) {
	  
		 //initialize two blank game states, (that match)
		 clearGameState(&G);
		 clearGameState(&G2);
		 //unmatch = 0;
		 
		 //get random player number 2-4, and assign them to the appropriate turn in the game states.
		 currentPlayer = (int)(Random()*2)+2;
		 G.whoseTurn = G2.whoseTurn = currentPlayer;
		 
		 //get random hand size and assign in both game states
		 handCount = (int)(Random()*MAX_HAND);
		 G.handCount[currentPlayer] = G2.handCount[currentPlayer] = handCount;
		 
		 //get random cards to fill these handcounts and assign in the game states
		 for (j=0; j<handCount; j++) {
		  G.hand[currentPlayer][j] = G2.hand[currentPlayer][j] = (int)(Random()*MAX_HAND); 
		 }
		 
		 //get random deck size and assign in both game states (and make sure it is at least >= 3)
		 deckCount = (int)((Random()*MAX_HAND)+3);
		 G.deckCount[currentPlayer] = G2.deckCount[currentPlayer] = deckCount;
		 
		 //get random cards to fill these deckcounts and assign in the game states
		 for (j=0; j<deckCount; j++) {
		  G.deck[currentPlayer][j] = G2.deck[currentPlayer][j] = (int)(Random()*MAX_HAND); 
		 }
		 
		 //put council room card in each player's hand.
		 G.handCount[currentPlayer]++;
		 G2.handCount[currentPlayer]++;
		 G.hand[currentPlayer][handCount] = G2.hand[currentPlayer][handCount] = council_room;
		 
		 //Call Council Room function on ONLY GameState 1.
		 cardEffect(council_room, 0, 0, 0, &G, handCount, NULL);
		 
		 //Now that G has had the Council Room card played, test ALL things done by the Council Room Card by comparing G and G2
		 //Test that the player has three more cards than before (four were added, one was discarded)
		 if (debug) printf("G1 handcount: %d	G2 handcount: %d\n", G.handCount[currentPlayer], G2.handCount[currentPlayer]);
		 if (G.handCount[currentPlayer] != G2.handCount[currentPlayer]+3) {
			printf("Error: Player that called Council should have %d cards in hand, instead they have %d\n", G2.handCount[currentPlayer]+3, G.handCount[currentPlayer]);
			errors++;
		 }
		 
		 //Test that the number of buys was incremented by one
		 if (G.numBuys != G2.numBuys + 1) {
			printf("Error: numBuys was not incremented properly\n");
			errors++;
		 }
		 
		 //Test that every other player now has an additional card
		 for (k=0; k<G.numPlayers; k++) {
			if (k != currentPlayer) {
				if (G.handCount[k] != G2.handCount[k] + 1) {
					printf("Error: Other players did not gain one card properly. Expected %d cards in hand, instead got %d\n", G2.handCount[i]+1, G.handCount[i]);
					errors++;
				}
			}
		 }
		 
		 //Test that playedCardCount was incremented properly
		 if (G.playedCardCount != G2.playedCardCount + 1) {
			printf("Error: PlayedCardCount was not incremented properly\n");
			errors++;
		 }

	} //end main loop
	
	printf("Out of %d tests, %d errors were returned\n\n", num_tests, errors);
  
  return 0;
}

/* for reference
case council_room:
      //+4 Cards
      for (i = 0; i < 4; i++)
	{
	  drawCard(currentPlayer, state);
	}
			
      //+1 Buy
      state->numBuys++;
			
      //Each other player draws a card
      for (i = 0; i < state->numPlayers; i++)
	{
	  if ( i != currentPlayer )
	    {
	      drawCard(i, state);
	    }
	}
			
      //put played card in played card pile
      discardCard(handPos, currentPlayer, state, 0);
			
      return 0;
*/